package com.zgs.mall.coupon.service.impl;

import com.zgs.common.to.MemberPrice;
import com.zgs.common.to.SkuReductionTo;
import com.zgs.mall.coupon.entity.MemberPriceEntity;
import com.zgs.mall.coupon.entity.SkuLadderEntity;
import com.zgs.mall.coupon.service.MemberPriceService;
import com.zgs.mall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.coupon.dao.SkuFullReductionDao;
import com.zgs.mall.coupon.entity.SkuFullReductionEntity;
import com.zgs.mall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void savaSkuReductionInfo(SkuReductionTo skuReductionTo) {
        Long skuId = skuReductionTo.getSkuId();
        //满减 fullCount
        BigDecimal fullPrice = skuReductionTo.getFullPrice();
        BigDecimal reducePrice = skuReductionTo.getReducePrice();
        if(fullPrice.compareTo(new BigDecimal(0))==1  && reducePrice.compareTo(new BigDecimal(0))==1 ){
            int priceStatus = skuReductionTo.getPriceStatus();
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuId);
            skuFullReductionEntity.setFullPrice(fullPrice);
            skuFullReductionEntity.setReducePrice(reducePrice);
            skuFullReductionEntity.setAddOther(priceStatus);
           this.baseMapper.insert(skuFullReductionEntity);
        }
        //打折
        BigDecimal discount = skuReductionTo.getDiscount();
        int fullCount = skuReductionTo.getFullCount();
        if (discount.compareTo(new BigDecimal(0))==1 && fullCount>0){
            int countStatus = skuReductionTo.getCountStatus();
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setDiscount(discount);
            skuLadderEntity.setFullCount(fullCount);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }
        //会员价
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice !=null && !memberPrice.isEmpty()){
            List<MemberPriceEntity> memberPriceEntityList = memberPrice.stream().map(me -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                Long id = me.getId();
                String name = me.getName();
                BigDecimal price = me.getPrice();
                memberPriceEntity.setMemberLevelId(id);
                memberPriceEntity.setMemberLevelName(name);
                memberPriceEntity.setMemberPrice(price);
                memberPriceEntity.setSkuId(skuId);
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntityList);
        }
    }

}